//
//  CCCityViewController.m
//  Party
//
//  Created by its on 15/3/16.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCCityViewController.h"
#import "CCCityViewModel.h"
#import "CCDevice.h"
@interface CCCityViewController ()
@property(nonatomic, weak) CCCityViewModel *viewModel;
@end
@implementation CCCityViewController

- (id) initWithViewModel:(CCCityViewModel *)viewModel {
  if (self = [super init]) {
    _viewModel = viewModel;
    _viewModel.viewDidLoadSignal = [self rac_signalForSelector:@selector(viewDidLoad)];
  }
  return self;
}

- (void) viewDidLoad {
  [self setupTitleBar];
  [self setupTableView];
  [self.viewModel.finishSignal subscribeNext:^(id x) {
    [self.tableView reloadData];
  } error:^(NSError *error) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:error.domain
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil, nil];
    [alertView show];
  } completed:^{
    
  }];
  [super viewDidLoad];
}

- (void)loadView {
  UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  view.backgroundColor = [UIColor redColor];
  self.view = view;
  
}

- (void) setupTitleBar {
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  [button setImage:[UIImage imageNamed:@"close_icon"] forState:UIControlStateNormal];
  [button setImage:[UIImage imageNamed:@"close_icon_highlighted"] forState:UIControlStateHighlighted];
  button.rac_command = _viewModel.closeCommand;
  button.frame = CGRectMake(0, 0, 24, 24);
  UIBarButtonItem *closeBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  self.navigationItem.leftBarButtonItem = closeBarButton;
  
  UISearchBar *bar = [[UISearchBar alloc] init];
  RAC(bar, placeholder) = RACObserve(_viewModel, placeholdString);
  self.navigationItem.titleView = bar;
  bar.delegate = self;
}

- (void) setupTableView {
  _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  [self.view addSubview: _tableView];  
  [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.size.equalTo(self.view);
    make.top.equalTo(self.view);
    make.left.equalTo(self.view);
  }] ;
  _tableView.delegate = self;
  _tableView.dataSource = self;

}

#pragma mark searchbar
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
  [self.viewModel beginSearch];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
  [self.viewModel endSearch];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  [self.viewModel searchWithString: searchBar.text];
}

#pragma  mark tableviewdelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

  NSArray *city =  self.viewModel.searchResultDic[_viewModel.searchArray[section]];
  NSLog(@"$$$$:%d",city.count);
  return  city.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return  self.viewModel.searchArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  }
  NSString *key = self.viewModel.searchArray[[indexPath section]];
  NSArray *cityArray = self.viewModel.searchResultDic[key];
  cell.textLabel.text = cityArray[[indexPath row]];
  return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEVICEWIDTH, 20)];
  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 20)];
  label.text = self.viewModel.searchArray[section];
  [view addSubview: label];
  view.backgroundColor = [UIColor lightGrayColor];
  return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return 20;
}
@end
